package com.oyb.servlet;

import com.oyb.entity.Admin;
import com.oyb.service.AdminServies;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/updateadmin")
public class UpdateAdmin extends HttpServlet {
    private AdminServies adminServies = new AdminServies();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String username= req.getParameter("username");
        String password = req.getParameter("password");
        String birthday = req.getParameter("birthday");
        String[] power = req.getParameterValues("power");

        //因为下面实例化对象的时候要用到date.所以这里拿到try外面
        //new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Admin admin = new Admin();
        admin.setId(Integer.parseInt(id));
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setBirthday(date);
        admin.setPower(power);

        int resulet = adminServies.upadmin(admin);
        if(resulet>0){
            req.getSession().setAttribute("login_user",admin);
            //修改成功
            resp.sendRedirect("index.jsp");
        }else{
            resp.sendRedirect("updateadmin.jsp?status=1");
        }
    }
}
